# -*- coding: utf-8 -*-
__author__ = 'yangq'

from appcommon.appinstance import mysql_app_pool
from common import timetool
from tornado import gen


@gen.coroutine
def get_pub_consut_list():
    sql = (
        "select * from pub_consult order by add_time desc"
    )
    info = yield mysql_app_pool.get_all(sql, cursor='dict')
    raise gen.Return(info)


@gen.coroutine
def get_user_add_school_list(area, school):
    param = ""
    if area:
        param += u"and area like '%{}%' ".format(area)
    if school:
        param += u"and school like '%{}%' ".format(school)
    sql = (
        u"select * from user_driver_school where 1=1 {} order by add_time desc"
    )
    info = yield mysql_app_pool.get_all(sql.format(param), cursor='dict')
    raise gen.Return(info)


@gen.coroutine
def get_driver_school_list(area, school):
    param = ""
    if area:
        param += u"and area like '%{}%' ".format(area)
    if school:
        param += u"and school like '%{}%' ".format(school)
    sql = (
        u"select * from driver_school where 1=1 {} order by id "
    )
    info = yield mysql_app_pool.get_all(sql.format(param), cursor='dict')
    raise gen.Return(info)


@gen.coroutine
def del_driver_school(id):
    sql = (
        "delete from driver_school where id = {}"
    )
    info = yield mysql_app_pool.delete(sql.format(id))
    raise gen.Return(info)


@gen.coroutine
def add_driver_school(area, school):
    current_time = timetool.get_timestamp()
    sql = (
        u"insert driver_school (area, school, add_time, add_user) values ('{}', '{}', {}, 'admin')"
    )
    info = yield mysql_app_pool.insert_one(sql.format(area, school, current_time))
    raise gen.Return(info)
